package com.sise.parkinglot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.parkinglot.bean.CodeMsg;
import com.sise.parkinglot.composition.PublicService;
import com.sise.parkinglot.entity.Admin;
import com.sise.parkinglot.entity.AdminLogin;
import com.sise.parkinglot.mapper.AdminLoginMapper;
import com.sise.parkinglot.mapper.AdminMapper;
import com.sise.parkinglot.service.IAdminService;
import com.sise.parkinglot.utils.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ZJ
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private AdminLoginMapper adminLoginMapper;

    @Resource
    private PublicService publicService;

    @Override
    public CodeMsg<String> login(Admin admin) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.lambda().eq(Admin::getUsername, admin.getUsername());
        Admin getAdmin = adminMapper.selectOne(adminQueryWrapper);
        String password = publicService.md5Encryption(admin.getPassword());
        AdminLogin adminLogin = new AdminLogin().setUsername(admin.getUsername())
                .setPassword(password).setLoginTime(LocalDateTime.now());
        if (getAdmin != null) {
            if (getAdmin.getPassword().equals(password)) {
                String msg = "登陆成功";
                adminLogin.setMsg(msg);
                adminLoginMapper.insert(adminLogin);
                Map<String, String> map = new HashMap<>();
                map.put("id", String.valueOf(getAdmin.getId()));
                String token = JwtUtil.createToken(map);
                return new CodeMsg<String>().setCode(200).setMsg(msg).setData(token);
            } else {
                String msg = "密码错误";
                adminLogin.setMsg(msg);
                adminLoginMapper.insert(adminLogin);
                return new CodeMsg<String>().setCode(401).setMsg(msg);
            }
        } else {
            String msg = "账号不存在";
            adminLogin.setMsg(msg);
            adminLoginMapper.insert(adminLogin);
            return new CodeMsg<String>().setCode(402).setMsg(msg);
        }
    }

    @Override
    public CodeMsg<String> addAdmin(Admin admin) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.lambda().eq(Admin::getUsername, admin.getUsername());
        Admin getAdmin = adminMapper.selectOne(adminQueryWrapper);
        if (getAdmin != null) {
            return new CodeMsg<String>().setCode(400).setMsg("账号已存在");
        } else {
            admin.setPassword(publicService.md5Encryption(admin.getPassword()));
            int insert = adminMapper.insert(admin);
            if (insert == 1) {
                return new CodeMsg<String>().setCode(200).setMsg("添加成功");
            } else {
                return new CodeMsg<String>().setCode(401).setMsg("添加失败");
            }
        }
    }
}
